(function () {
    /**
     * 大厅管理器
     * 包含 注册大厅资源，创建房间入口，绑定事件，更新数据等
     */
    function HomeController() {
        this.Stage = null;
        this.HomeUI = null;
        this.HomeBG = null;
        this.Menu = null;
        this.RoomEntry = [];

        viewer.register("arena.start", this.start.bind(this));
        viewer.register("arena.resize", this.resize.bind(this));
        viewer.register("home.loaded", this.init.bind(this));
        viewer.register("menu.status", this.menuStatusChange.bind(this));
        viewer.register("user::getUserAmount", this.updateUserInfo.bind(this));
        viewer.register("room::list", this.updateRoomInfo.bind(this));
    }

    var _proto = HomeController.prototype;
    /**
     * @privite
     * 注册大厅字体资源
     */
    _proto.regAssets = function () {
        var bitmapFont = new Laya.BitmapFont();
        bitmapFont.loadFont("res/font/font_ante.fnt");
        Laya.Text.registerBitmapFont("font_ante", bitmapFont);
    }
    /**
     * @privite
     * 创建大厅界面UI，房间入口UI
     */
    _proto.createUI = function () {
        var stage = this.Stage;
        var RoomEntry = this.RoomEntry = [];
        var HomeUI = new ui.Home.HomePageUI();
        var BG = new ui.Home.HomeBGUI();
        var Menu = new ui.Home.Menu();

        HomeUI.zOrder = 2;
        BG.width = stage.width;
        BG.height = stage.height;

        HomeUI.addChild(Menu);
        //舞台添加大厅主界面
        stage.addChildren(BG, HomeUI);

        this.HomeBG = BG;
        this.HomeUI = HomeUI;
        this.Menu = Menu;

        //添加房间入口，并绑定点击事件
        for(var i = 1; i <= 3; i++){
            var level = new ui.Com.RoomLevel(i, function (roomId) {
                utils.log("点击了房间：" + roomId);

                if(!Arena.UserLogged){
                    location.href = Arena.LoginUrl;
                    return;
                }
                
                viewer.publish("loader.load", "room", roomId);
            });
            
            RoomEntry.push(level);
            stage.addChild(level);
        }
    }
    /**
     * @privite
     * 大厅元素绑定绑定事件
     */
    _proto.bindEvent = function () {
        var HomeUI = this.HomeUI;

        // new ui.Com.BtnSound(HomeUI.btnSound);

        // Laya 豆哥计算
        if(window.GM && GM.isCall_out === 1 && GM.popBalanceShow_out){
            // 豆哥按钮绑定事件
            HomeUI.gameScore.on(Laya.Event.CLICK, this, function(){
                GM.popBalanceShow_out();

                utils.log("点击了豆哥计算按钮");
            });
        }

        HomeUI.btnBack.on(Laya.Event.CLICK, this, function(){
            window.GM && GM.btnBackCall_out();
            
            utils.log("点击了返回按钮");
        });

        HomeUI.btnRank.on(Laya.Event.CLICK, this, function(){
            viewer.publish("alert.show", "rank");
            
            utils.log("点击了排行榜按钮");
        });

        HomeUI.btnHome.on(Laya.Event.CLICK, this, function(){
            utils.log("点击了主页按钮");
            location.href = GM.backHomeUrl;
        });
        HomeUI.btnShowMenu.on(Laya.Event.CLICK, this, function(){
            this.Menu.showMenu();

            utils.log("点击了菜单按钮");
        });

        HomeUI.btnRecharge.on(Laya.Event.CLICK, this, function(){
            if(GM.appStorePay == 1 && (window.wltgame && wltgame.suportStorePay == 1) || GM.appStorePay == 2){
                location.href = "/?act=ios_applepay&st=applepay_game_vertical&gameId=" + GM.gameId + "&redirect_uri=" + redirect_uri;
            }else{
                viewer.publish("alert.show", "recharge");
            }

            utils.log("点击了充值按钮");
        });

        HomeUI.btnCashout.on(Laya.Event.CLICK, this, function(){
            viewer.publish("alert.show", "cashout");

            utils.log("点击了兑出按钮");
        });

        HomeUI.btnQuickStart.on(Laya.Event.CLICK, this, function () {
            if(!Arena.UserLogged){
                location.href = Arena.LoginUrl;
                return;
            }
            
            viewer.publish("loader.load", "room");

            utils.log("点击了快速开始按钮");
        });

        if (window.GM && GM.isCall_out === 1 && GM.isShowBtnBack_out && GM.btnBackCall_out) {
            HomeUI.btnBack.visible = true; // 显示返回按钮
        };

        if(window.GM && GM.backHomeUrl != ""){
            HomeUI.btnHome.visible = true; // 显示主页按钮
        }
    }
    /**
     * @privite
     * 推送获取房间信息的命令
     */
    _proto.getHomeInfo = function () {
        viewer.publish("io.emit", {
            cmd: "user::getUserAmount",
            params : {
                token : token
            }
        });
        viewer.publish("io.emit", {cmd: "room::list"});
    }
    /**
     * @public
     * 接收user::getUserAmount消息，更新大厅中用户的信息
     * 
     * @param {Object} userInfo 用户信息 platformAmount:平台余额，gameAmount:游戏币
     */
    _proto.updateUserInfo = function (userInfo) {
        var HomeUI = this.HomeUI;

        Arena.userInfo = userInfo;
        HomeUI.gameScore.text = userInfo.platformAmount;
        HomeUI.coinScore.text = userInfo.gameAmount;
    }
    /**
     * @public
     * 接收room::list消息，更新房间入口信息
     * 
     * @param {Array} roomInfo 三个房间的信息，包含每个房间的roomId，最少携带额，在线人数，最小压注额
     */
    _proto.updateRoomInfo = function (roomInfo) {
        var RoomEntry = this.RoomEntry;
        for(var i in roomInfo){
            RoomEntry[i].update({
                roomId  : roomInfo[i].roomid,
                ante    : roomInfo[i].bottom,
                against : roomInfo[i].min,
                online  : roomInfo[i].count,
                isOpen  : !!roomInfo[i].open
            });
        }
    }
    /**
     * @public
     * 接收home.loaded消息，已经加载完大厅资源，初始化大厅界面
     */
    _proto.init = function () {
        Arena.Stage.removeChildren();
        
        this.regAssets();
        this.createUI();
        this.bindEvent();
        this.getHomeInfo();
    }
    /**
     * @public
     * 游戏开始时监听arena.start消息，发布加载大厅资源事件，携带参数true，表明第一次加载大厅界面
     */
    _proto.start = function () {
        this.Stage = Arena.Stage;
    }
    /**
     * @public
     * 监听arena.resize消息，重置界面位置
     */
    _proto.resize = function () {
        if(!this.HomeBG){return;}
        this.HomeBG.width = Laya.stage.width;
        this.HomeBG.height = Laya.stage.height;

        this.HomeUI.centerX = 0;

        for(var i in this.RoomEntry){
            this.RoomEntry[i].centerX = 0;
        }
    }
    _proto.menuStatusChange = function (status) {
        if (status == "open") {
            this.HomeUI.btnShowMenu.skin = "res/home/btn_show_menu_open.png";
        }else{
            this.HomeUI.btnShowMenu.skin = "res/home/btn_show_menu_close.png";
        }
    }

    new HomeController();
})();